﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Server.ProjectHelpers;
using Server;

namespace Server.GUI
{
    public partial class ProjectObserver : UserControl
    {
        protected int _projectId = 0;
        protected Controller _controller;

        public Controller Controller
        {
            set
            {
                _controller = value;
            }
        }
        public ProjectObserver()
        {
            
          
            InitializeComponent();
        }

        public void SetProject(int projectId)
        {
            this._projectId = projectId;
            this.LoadData();
            this._controller.AddProjectObserver(projectId, this);
        }

        public void ProjectOnChange(object sender, EventArgs e)
        {
            ProjectDecorator project = (ProjectDecorator)sender;
            if (project.Id == _projectId)
            {
                if (project.Lib.CanGetProgrss)
                {
                    this.lProgress.Text = (project.Progress * 100).ToString() + "%";
                    this.pbProgress.Value = (int)(project.Progress * 100);
                }
                this.LoadData();
            }
        }

        private void LoadData()
        {
            var connection = new ServerDataBaseContainer();
            Project project = connection.ProjectSet.First(p => p.Id == _projectId);
            this.lAddDate.Text = project.AddDate.ToString();
            this.lComputingCount.Text = project
                                            .Compiuting
                                            .Count(c => c.CompiutingStatus.Id == (short)CompiutingStatuses.InProgress)
                                            .ToString()
                                        + "/"
                                        + project
                                            .Compiuting
                                            .Count()
                                            .ToString();
            this.lMaxDysOfComputing.Text = project.MaxDaysOfCompiuting.ToString();
            this.lName.Text = project.Name;
            this.lStatus.Text = project.Status.Name;
            this.numPriorytet.Value = project.Priorytet;

        }

        private void numPriorytet_ValueChanged(object sender, EventArgs e)
        {
            this._controller.SetPriorytet(_projectId, (byte)this.numPriorytet.Value);
        }

    }
}
